package com.bestcem.xm.ticket.service.dto;

import com.alibaba.fastjson.JSONObject;
import com.bestcem.xm.ticket.controller.vo.RespondentVO;
import com.bestcem.xm.ticket.entity.mongo.Record;
import com.bestcem.xm.ticket.entity.mongo.TicketRule;
import com.bestcem.xm.ticket.grpc.client.dto.qdes.QdesDTO;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author XiaoYunSong <ys.xiao@idiaoyan.com>
 * @version v1.0
 * @date 2021/9/1 15:21
 */
@Data
public class RecordDTO {

    private static final long serialVersionUID = -3578092062110162825L;

    @JsonProperty("id")
    private String id;

    /**
     * 公司ID
     */
    @JsonProperty("orgID")
    private String orgId;

    /**
     * 预警生成方式
     *
     * @see com.bestcem.xm.ticket.enums.RecordModeEnum
     */
    @JsonProperty("mode")
    private Integer mode;

    /**
     * 问卷系统ID
     */
    @JsonProperty("qprojectID")
    private String qProjectId;

    /**
     * 问卷系统ID
     */
    @JsonProperty("qproject")
    private QdesDTO qProject;

    /**
     * 答卷编号
     */
    @JsonProperty("rspdSeq")
    private Long rspdSeq;

    /**
     * 预警编号 ="A"+str(rspdSeq)
     */
    @JsonProperty("alertSeq")
    private String alertSeq;

    /**
     * 答卷归属组及其祖先组
     */
    @JsonProperty("groupList")
    private List<String> groupList;

    /**
     * 答卷归属组编码
     */
    @JsonProperty("groupCode")
    private String groupCode;

    /**
     * 触发的规则的condition
     */
    @JsonProperty("ruleList")
    private List<TicketRule.Condition> ruleList;

    /**
     * 预警关闭时间
     */
    @JsonProperty("closeDT")
    private Date closeDt;

    /**
     * 预警关闭原因
     *
     * @see com.bestcem.xm.ticket.enums.RecordCloseReasonEnum
     */
    @JsonProperty("closeReason")
    private Integer closeReason;

    /**
     * 触发预警的内容
     */
    private JSONObject data;

    /**
     * group
     */
    @JsonProperty("group")
    private Group group;

    /**
     * rspd
     */
    @JsonProperty("rspd")
    private RespondentVO rspd;

    /**
     * 数据记录标记
     */
    @JsonProperty("record_flag")
    private Integer recordFlag = 1;

    @JsonProperty("created_dt")
    private Date createdDate;

    @JsonProperty("created_id")
    private String createdId;

    @JsonProperty("updated_dt")
    private Date updatedDate;

    @JsonProperty("updated_id")
    private String updatedId;

    @Data
    public static class Group {
        @JsonProperty("code")
        private String code;

        @JsonProperty("name")
        private String name;

        public Group() {
            this.code = "";
            this.name = "";
        }
    }

    public static RecordDTO getInstance(Record record) {
        RecordDTO recordDTO = new RecordDTO();
        recordDTO.setId(record.getId());
        recordDTO.setRecordFlag(record.getRecordFlag());
        recordDTO.setCreatedDate(record.getCreatedDate());
        recordDTO.setCreatedId(record.getCreatedId());
        recordDTO.setUpdatedDate(record.getUpdatedDate());
        recordDTO.setUpdatedId(record.getUpdatedId());
        recordDTO.setOrgId(record.getOrgId());
        recordDTO.setMode(record.getMode());
        recordDTO.setQProjectId(record.getQProjectId());
        recordDTO.setRspdSeq(record.getRspdSeq());
        recordDTO.setAlertSeq(record.getAlertSeq());
        recordDTO.setGroupList(record.getGroupList());
        recordDTO.setGroupCode(record.getGroupCode());
        recordDTO.setRuleList(record.getRuleList());
        List<TicketRule.Condition> ruleList = recordDTO.getRuleList();
        if (!CollectionUtils.isEmpty(ruleList)) {
            for (TicketRule.Condition condition : ruleList) {
                if (Objects.isNull(condition.getRuleType())) {
                    condition.setRuleType(1);
                }
            }
            recordDTO.setRuleList(ruleList);
        }
        recordDTO.setCloseDt(record.getCloseDate());
        recordDTO.setCloseReason(record.getCloseReason());
        recordDTO.setData(record.getData());
        return recordDTO;
    }

}
